package com.zs.leymovie.activity;

import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.zs.leymovie.R;

public class MyActivity extends AppCompatActivity {
    private ImageView iv_rocket;
    private ImageView iv_cloud;
    private ImageView iv_cloud_line;
    private RelativeLayout rl_cloud;
    private  ImageView iv;
    private AnimationDrawable fireAnimationDrawable;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        iv = (ImageView) findViewById(R.id.iv_rocket);
        iv_cloud = (ImageView) findViewById(R.id.iv_cloud);
        iv_cloud_line = (ImageView) findViewById(R.id.iv_cloud_line);
        rl_cloud = (RelativeLayout) findViewById(R.id.rl_cloud);
    }

    @Override
    protected void onStart() {
        super.onStart();
        iv.setBackgroundResource(R.drawable.rocket_fire);
        fireAnimationDrawable = (AnimationDrawable) iv.getBackground();
        fireAnimationDrawable.start();
        fly();
    }

    /**
     * 火箭飞起来的动画，同时下方播放冒烟的动画
     */
    private void fly() {
        Animation animation = AnimationUtils.loadAnimation(
                this.getApplicationContext(), R.anim.rocket_up);
        animation.setFillAfter(true);

        animation.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {
                // 开始发射的时候去播放动画
                MediaPlayer player = MediaPlayer.create(MyActivity.this, R.raw.rocket);
                player.setLooping(false);
                player.setVolume(1.0f, 1.0f);
                player.start();

                AlphaAnimation cloudAppearAnimation = new AlphaAnimation(0.1f,
                        1.0f);
                cloudAppearAnimation.setDuration(2000);

                Animation appearAnimation = new AlphaAnimation(0.0f, 1.0f);
                appearAnimation.setDuration(3500);
                appearAnimation.setStartOffset(250);

                iv_cloud.startAnimation(cloudAppearAnimation);
                iv_cloud_line.startAnimation(appearAnimation);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                // 火箭播放完成后就去把云彩都消失
                removeClound();
               /* if (!ServiceUtil.isServiceRunning(MyActivity.this,RocketService.class)) {
                    startService(new Intent(MyActivity.this.getApplicationContext(), RocketService.class));
                }*/
            }
        });

        iv.startAnimation(animation);
        createClound();

    }
    private void removeClound() {
        AlphaAnimation disappearAnimation = new AlphaAnimation(1.0f, 0.0f);
        disappearAnimation.setDuration(1000);
        disappearAnimation.setFillAfter(true);
        disappearAnimation
                .setInterpolator(new AccelerateDecelerateInterpolator());

        disappearAnimation.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                //iv.setVisibility(View.GONE);
                fireAnimationDrawable.stop();
                iv_cloud.setVisibility(View.GONE);
                iv_cloud_line.setVisibility(View.GONE);
                MyActivity.this.finish();
            }
        });
        rl_cloud.startAnimation(disappearAnimation);
    }

    private void createClound() {
        iv_cloud.setVisibility(View.VISIBLE);
        iv_cloud_line.setVisibility(View.VISIBLE);
    }
}
